/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package nuevoingresoA;

import conexion.Conexion;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 *
 * @author PACHITO
 */
@WebServlet(name = "agregarnuevoalumno", urlPatterns = {"/agregarnuevoalumno"})
public class agregarnuevoalumno extends HttpServlet {

    Conexion conecta = new Conexion();
    Statement sentenciaSQL = null;
    ResultSet consulta = null;

    @Override
    public void init(ServletConfig config) throws ServletException {
        super.init(config);
        conecta.Conectar();
        sentenciaSQL = conecta.getSentenciaSQL();
    }

    /**
     * Processes requests for both HTTP
     * <code>GET</code> and
     * <code>POST</code> methods.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        try {
            /* TODO output your page here. You may use following sample code. */
            out.println("<html>");
            out.println("<head>");
            out.println("<title>Servlet agregarnuevoalumno</title>");
            out.println("</head>");
            out.println("<body>");
            out.println("<h1>Servlet agregarnuevoalumno at " + request.getContextPath() + "</h1>");
            out.println("</body>");
            out.println("</html>");
        } finally {
            out.close();
        }
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP
     * <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
    }

    /**
     * Handles the HTTP
     * <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        try {
            String nom, tutor, app, apm, calle, numero, colonia, cp, curp, fechan, sexoo, telcasa, telcel, corre, escuelapro, grupoo, nivel;
            String nivelll = "";
            String anio;
            anio = request.getParameter("anio");
            nom = request.getParameter("nom");
            app = request.getParameter("app");
            apm = request.getParameter("apm");
            calle = request.getParameter("calle");
            numero = request.getParameter("numero");
            colonia = request.getParameter("colonia");
            cp = request.getParameter("cp");
            fechan = request.getParameter("fechan");
            sexoo = request.getParameter("sexo");
            telcasa = request.getParameter("telcasa");
            telcel = request.getParameter("telcel");
            corre = request.getParameter("correo");
            escuelapro = request.getParameter("escuelapro");
            grupoo = request.getParameter("grupoo");
            curp = request.getParameter("curp");
            nivel = request.getParameter("nivel");
            tutor = request.getParameter("tutor");
            int nivell = Integer.parseInt(nivel);
            if (nivell == 1) {
                nivelll = "preescolar";
            }
            if (nivell == 2) {
                nivelll = "primaria";
            }
            if (nivell == 3) {
                nivelll = "secundaria";
            }
            if (nivell == 4) {
                nivelll = "bachilleratog";
            }
            if (nivell == 5) {
                nivelll = "bachilleratot";
            }
            if (nivell == 6) {
                nivelll = "licenciatura";
            }
            if (nivell == 7) {
                nivelll = "maestria";
            }
            if (nivell == 8) {
                nivelll = "doctorado";
            }
            String resul = "";
            for (int i = 0; i < 3; i++) {
                resul += nivelll.charAt(i);
            }
            consulta = sentenciaSQL.executeQuery("select max(idalumno) from alumno");
            int matr = 0;
            while (consulta.next()) {
                matr = consulta.getInt(1) + 1;
            }
            String matricula = "" + resul + anio + matr;
            sentenciaSQL.executeUpdate("INSERT INTO `gutt`.`alumno` "
                    + "(`idalumno`, `matricula`, `nombre`, `app`, `apm`, `calle`, `numero`, `colonia`,"
                    + " `cp`, `telefono`, `curp`, `sexo`, `fechanac`, `telefonocel`, `correo`,`tutor`)"
                    + " VALUES"
                    //poner matricula automatica
                    + " (NULL, '" + matricula + "', '" + nom + "', '" + app + "', '" + apm + "',"
                    + " '" + calle + "', '" + numero + "', '" + colonia + "', '" + cp + "',"
                    + " '" + telcasa + "', '" + curp + "', '" + sexoo + "', '" + fechan + "', '" + telcel + "', '" + corre + "','" + tutor + "');");
//            sentenciaSQL.executeUpdate("INSERT INTO `gutt`.`niveleducativo` (`idnivel`, `idalumno`, `nivel`) "
//                    + "VALUES (NULL, '" + matr + "', '" + nivelll + "');");
             sentenciaSQL.executeUpdate("INSERT INTO `gutt`.`niveleducativo` (`idnivel`, `idnivell`, `idalumno`, `nivel`) "
                     + "VALUES (NULL, '"+nivel+"', '"+matr+"', '"+nivelll+"');");
           


            out.close();


        } catch (SQLException e) {
            out.println("Excepcion sql111111" + e);
            out.println("" + sentenciaSQL);

        } catch (NullPointerException e) {
            out.println("Apuntando sql hector" + e + sentenciaSQL);
            out.println("" + sentenciaSQL);

        }
    }

    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
